package org.example.novel.utlis;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.example.novel.config.WebMvcConfig;

import java.nio.charset.StandardCharsets;
import java.util.Date;

public class JwtUtils {

    private final Claims claims;

    private static final String KEY = WebMvcConfig.getKEY();

    private static final Date date = new Date(System.currentTimeMillis()+ 60000L);

    private static final String name = "object";

    public static String createToken(String name,Object value){
        return Jwts
                .builder()
                .claim(name,value)
                .setExpiration(date).signWith(SignatureAlgorithm.HS256,KEY.getBytes(StandardCharsets.UTF_8))
                .compact();
    }

    public static String createToken(Object value){
        return createToken(name,value);
    }

    public static JwtUtils parseToken(String token){
        Claims body = Jwts.parser()
                .setSigningKey(KEY.getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token).getBody();
        return new JwtUtils(body);
    }


    public static boolean verifyToken(String token){
        try {
            parseToken(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public JwtUtils(Claims claims) {
        this.claims = claims;
    }

    public Claims getClaims() {
        return claims;
    }

    public Object get(String name){
        return claims.get(name);
    }

}
